home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / MISC.SWG / 0035_Simple Multi-Tasker.pas < prev    next >
Pascal/Delphi Source File  |  1993-08-27  |  3KB  |  103 lines

  1. {
  2.  by Sean L. Palmer
  3.  Public Domain
  4.  
  5.  This is a 'multitasking' Program in the sense that it hooks into
  6.  the timer interrupt, but what that interrupt ends up actually
  7.  doing is controlled by the current value in SaveAdr, which
  8.  changes With each interrupt as the routine passes control back
  9.  to the tick handler not by Exiting normally, but by an explicit
  10.  transfer of control.
  11.  The end result of this is that you can Write a state-driven
  12.  interrupt handler
  13.  The included example is RealLY simplistic, and barely tested.
  14.  I intend to use this to Write a comm port driver that
  15.  parses the incoming data as it receives it which would
  16.  be nice in a communications Program that shells to Dos, as
  17.  the incoming Chars could be saved to disk in the background
  18.  With buffered ZModem or something...
  19. }
  20.  
  21. Program intTest;
  22.  
  23. Uses
  24.   Dos;
  25.  
  26. Var
  27.   saveAdr : Word;  {offset in this code segment of where we are now}
  28.   active  : Boolean;  {to avoid re-entrancy}
  29.  
  30. Procedure intHandler; Far; Assembler;
  31. Asm
  32.   pusha
  33.   mov  ax, seg @DATA
  34.   mov  ds, ax
  35.  
  36.   {anything you need to do before continuing (reading port data?), do here}
  37.  
  38.   in   al, $61  {click speaker as an example}
  39.   xor  al, 2
  40.   out  $61, al
  41.  
  42.   test active, $FF  {exit now if interrupted ourselves}
  43.   jz   @OK
  44.   popa
  45.   iret
  46.  
  47.  @OK:
  48.   inc Byte ptr active
  49.   sti
  50.   jmp [saveAdr]  {near jump to continue where handler last left off}
  51. end;
  52.  
  53. {call this Procedure from StateHandler to suspend execution Until next time}
  54.  
  55. Procedure wait; near; Assembler;
  56. Asm {wait For next interrupt}
  57.   pop Word ptr saveAdr  {save where to continue next time}
  58.   dec Byte ptr active
  59.   popa                  {restore caller regs}
  60.   iret
  61. end;
  62.  
  63. Const
  64.   c : Char = '.';
  65.  
  66. Procedure stateHandler;
  67. begin
  68. {
  69.  a stateHandler Procedure should never ever Exit (only by calling 'wait'),
  70.  shouldn't have any local Variables or parameters, and shouldn't call
  71.  'wait' With anything on the stack (like from a subroutine).
  72.  This routine is using the caller's (interrupted Program's) stack, so be
  73.  very very careful}
  74.  
  75.  Asm
  76.    pop bp  {clean up stack mess left by Turbo's Procedure header}
  77.  end;
  78.  {^ alternative method here is to init saveAdr to offset(proc)+3 and skip
  79.   the push bp; mov bp,sp altogether}
  80.  
  81.   Repeat  {this is an example only}
  82.     c := '@';
  83.     wait;
  84.     c := '.';
  85.     wait;
  86.   Until False;                {don't let it return normally!!}
  87. end;
  88.  
  89. Var
  90.   oldHook : Procedure;
  91.   i       : Integer;
  92.  
  93. begin
  94.   saveAdr := ofs(stateHandler);
  95.   getIntVec($1C, @oldHook);
  96.   setIntVec($1C, @intHandler);
  97.   For i := 1 to 1500 do
  98.     Write(c);
  99.   setIntVec($1C, @oldHook);
  100. end.
  101.  
  102.  
  103.